package com.bjt.wms.sys.controller;

import com.bjt.common.config.paragetter.BasePageRequest;
import com.bjt.wms.sys.model.Employee;
import com.bjt.wms.sys.service.EmployeeService;
import com.bjt.utils.R;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

public class EmployeeController extends Controller {

    @Inject
    private EmployeeService employeeService;

    /**
     * 分页查询人员信息列表
     */
    public void queryList(BasePageRequest<Employee> basePageRequest) {

        Kv cond = Kv.create();
        if (StrKit.notBlank(getPara("name"))) {
            cond.set("name", getPara("name"));
        }
        if (StrKit.notBlank(getPara("dept"))) {
            cond.set("dept", getPara("dept"));
        }
        if (StrKit.notBlank(getPara("duty"))) {
            cond.set("duty", getPara("duty"));
        }

        renderJson(employeeService.queryEmployeeList(basePageRequest, cond));
    }

    public void save() {
        Employee employee = getModel(Employee.class, "");
        if (employee.getId() == null) {
            renderJson(employee.save() ? R.ok() : R.error("保存人员信息失败"));
        } else {
            renderJson(employee.update() ? R.ok() : R.error("保存人员信息失败"));
        }
    }

    public void delete() {
        String[] idArray = getParaValues("id");
        String ids = StrKit.join(idArray, ",");
        renderJson(Db.update("delete from employee where id in (" + ids + ")") > 0 ? R.ok() : R.error("删除人员信息失败"));
    }
}
